package com.cyandpos.pad.bankpay;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.cyandpos.pad.activity.PayActivity;
import com.cyandpos.pad.common.commondata.Constant;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.YhkCardItem;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.log.MessageLog;

import java.util.ArrayList;

import static com.cyandpos.pad.common.commondata.Constant.PAY_ICBC_TRADS_RESULTID_BACK;

public class BankpayManger {

    private static BankpayManger bankpayManger;
    public PayActivity mContxt;
    private static final String APP_PAKEGE = "com.cyandpos.andpos";
    private static final String APP_PAY_VER = "1030000";
    private static final String SOFT_PAY_PAKEGE = "com.icbc.smartpos.transservice.startTrans";

    public static BankpayManger getInstance( ) {
        if (bankpayManger == null) {
            synchronized (BankpayManger.class) {
                if (bankpayManger == null) {
                    bankpayManger = new BankpayManger();
                }
            }
        }
        return bankpayManger;
    }

    public void setBankpayManger(PayActivity payActivity){
        this.mContxt= payActivity;
    }

    /**
     * 银行卡消费
     * */
    public void bankconsume(BankData bankData,String  bankname){

        if (bankname.equals("工行")){
            Intent intent = new Intent();
            intent.setAction(SOFT_PAY_PAKEGE);
            intent.putExtra("transType", "MULTI_PURCHASE");
            Bundle ctrlData = getCtrlData();
            Bundle transData = new Bundle();
            transData.putLong("AMOUNT", bankData.getAmount());
            transData.putStringArrayList("TRANS_LIST", getTransList(bankData.getPayType()));
            intent.putExtra("ctrlData", ctrlData);
            intent.putExtra("transData", transData);
            mContxt.startActivityForResult(intent, Constant.PAY_ICBC_TRADS_RESULTID);

        }else  if (bankname.equals("交行")){

            Intent intent = new Intent();
            intent.setAction(SOFT_PAY_PAKEGE);
            intent.putExtra("transType", "MULTI_PURCHASE");
            Bundle ctrlData = getCtrlData();
            Bundle transData = new Bundle();
            transData.putLong("AMOUNT", bankData.getAmount());
            transData.putStringArrayList("TRANS_LIST", getTransList(bankData.getPayType()));
            intent.putExtra("ctrlData", ctrlData);
            intent.putExtra("transData", transData);
            mContxt.startActivityForResult(intent, Constant.PAY_ICBC_TRADS_RESULTID);

        }else {


        }
    }
    /**
     * 银行卡隔日退
     * */
    public void bankback(BankData bankData,String  bankname){

        if (bankname.equals("工行")){
            Intent intent = new Intent();
            intent.setAction(SOFT_PAY_PAKEGE);
            intent.putExtra("transType", "REFUND");// 退货
            Bundle ctrlData = getCtrlData();
            Bundle transData = new Bundle();
            transData.putLong("AMOUNT", bankData.getAmount());
            transData.putBoolean("CARDINFO_CONFIRM",true);
            intent.putExtra("ctrlData", ctrlData);
            intent.putExtra("transData", transData);
            Log.i("AAD ",intent.toString());
            mContxt.startActivityForResult(intent, PAY_ICBC_TRADS_RESULTID_BACK);


        }else  if (bankname.equals("交行")){

            Intent intent = new Intent();
            intent.setAction(SOFT_PAY_PAKEGE);
            intent.putExtra("transType", "MULTI_PURCHASE");
            Bundle ctrlData = getCtrlData();
            Bundle transData = new Bundle();
            transData.putLong("AMOUNT", bankData.getAmount());
            transData.putStringArrayList("TRANS_LIST", getTransList(bankData.getPayType()));
            intent.putExtra("ctrlData", ctrlData);
            intent.putExtra("transData", transData);
            mContxt.startActivityForResult(intent, Constant.PAY_ICBC_TRADS_RESULTID);

        }else {


        }
    }

    public YhkCardItem onActivityResultBack(Intent data, int mSelectedPmtId, int payType,boolean type) {
        YhkCardItem xykItem = new YhkCardItem();
        // 银行卡返回
        if (null == data){
            //返回异常
            Toast.makeText(mContxt, "银行返回异常 ! ",Toast.LENGTH_SHORT).show();
        }
        MessageLog.writeLog("TAG----银行卡支付 ");
        Bundle baseResult = data.getBundleExtra("baseResult");
        Bundle transResult = data.getBundleExtra("transResult");
        Bundle extraInfo = data.getBundleExtra("extraInfo");
        //交易错误描述的信息 DESCRIPTION
        if (null != baseResult){
            Toast.makeText(mContxt, baseResult.getString("DESCRIPTION"),Toast.LENGTH_SHORT) .show();
        }else{
            Toast.makeText(mContxt, "银行返回异常，进行交易查询补录",Toast.LENGTH_SHORT).show();
        }
        if (type){// 消费
            if (baseResult.getLong("RESULT")==0){
                String shilded_pan = extraInfo.getString("SHILDED_PAN"); //卡号
                String  transTime= baseResult.getString("TRANS_TIME");
                String pay_type = transResult.getString("PAY_TYPE");
                long  amount= transResult.getLong("AMOUNT");
                long  ret_amt= transResult.getLong("RET_AMT");
                String amt = String.valueOf(amount*1.0/100); //消费时的金额
                String ref_no = transResult.getString("REF_NO");//检索参考号
                String trace_no = transResult.getString("TRACE_NO");//终端流水号
                MessageLog.writeLog("银行返回参数值：  "+pay_type+"  ;  "+amt+" ;  "+ amount+"   ; "+ref_no+"  ;  "+trace_no+"  ;  "+transTime+"  ;  ");
                xykItem.setAmount(MoneyUtils.getMoneyStr((int) amount));
                xykItem.setLsh(trace_no);
                xykItem.setPzh(ref_no);
                xykItem.setCkh(transTime);
                xykItem.setCardNo(shilded_pan);
                xykItem.setYhje("0");
                xykItem.setYhje_shop("0");
                xykItem.setType(payType+"");
            }

        }else {

            if (baseResult.getLong("RESULT")==0){
                String shilded_pan = extraInfo.getString("SHILDED_PAN"); //卡号
                String  transTime= baseResult.getString("TRANS_TIME");
                String pay_type = transResult.getString("PAY_TYPE");
                long  amount= transResult.getLong("AMOUNT");
                long  ret_amt= transResult.getLong("RET_AMT");
                String amt = String.valueOf(amount*1.0/100); //消费时的金额
                String ref_no = transResult.getString("REF_NO");//检索参考号
                String trace_no = transResult.getString("TRACE_NO");//终端流水号
                MessageLog.writeLog("银行返回参数值：  "+pay_type+"  ;  "+amt+" ;  "+ amount+"   ; "+ref_no+"  ;  "+trace_no+"  ;  "+transTime+"  ;  ");

                xykItem.setAmount(MoneyUtils.getMoneyStr((int) amount));
                xykItem.setLsh(trace_no);
                xykItem.setPzh(ref_no);
                xykItem.setCkh(transTime);
                xykItem.setCardNo(shilded_pan);
                xykItem.setYhje("0");
                xykItem.setYhje_shop("0");
                xykItem.setType(payType+"");
            }

        }




        return  xykItem;

    }



    private Bundle getCtrlData() {
        Bundle ctrlData = new Bundle();
        ctrlData.putString("APP_NAME",APP_PAKEGE);
        ctrlData.putLong("BANKPAY_VER", Long.parseLong(APP_PAY_VER));
        return ctrlData;
    }

    private ArrayList<String> getTransList(int payType) {
        ArrayList<String> arrayList = new ArrayList<>();
        if(payType == Payment.SKFS_YHK){
            arrayList.add("PURCHASE");
        }/*else if(payType == Payment.SKFS_BANK_SMF){
            arrayList.add("QRPURCHASE");//二维码消费
        }  */

        return arrayList;
    }


}
